home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / fbzx.lha / fbzx / emulator.c < prev    next >
C/C++ Source or Header  |  2004-08-26  |  6KB  |  280 lines

  1. #include "Z80.h"
  2. #include "computer.h"
  3. #include "emulator.h"
  4. #include "cargador.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. //#include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include "characters.h"
  11. #include "menus.h"
  12. #include <string.h>
  13. #include <SDL/SDL.h>
  14. #include <SDL/SDL_thread.h>
  15. #include "sound.h"
  16. #include "tape.h"
  17. #include "microdrive.h"
  18.  
  19. Z80 procesador;
  20. struct computer ordenador;
  21. SDL_Surface *screen;
  22. char salir;
  23. unsigned char *sound[NUM_SNDBUF];
  24. char path_snaps[2049];
  25. char path_taps[2049];
  26. char path_mdrs[2049];
  27.  
  28. #ifdef AMIGA
  29. #include <proto/dos.h>
  30. static inline void sleep(unsigned int secs)
  31. {
  32.    Delay(secs*50);
  33. }
  34. #endif
  35.  
  36. void load_rom(char type) {
  37.  
  38.     FILE *fichero;
  39.     int size;
  40.  
  41.     switch(type) {
  42.     case 0:
  43. //        fichero=fopen("/usr/share/spectrum/roms/spectrum.rom","r");
  44.         fichero=fopen("roms/spectrum.rom","r");
  45.         size=16384;
  46.     break;
  47.     case 1:
  48. //        fichero=fopen("/usr/share/spectrum/roms/128.rom","r");
  49.         fichero=fopen("roms/128.rom","r");
  50.         size=32768;
  51.     break;
  52.     case 2:
  53. //        fichero=fopen("/usr/share/spectrum/roms/plus2.rom","r");
  54.         fichero=fopen("roms/plus2.rom","r");
  55.         size=32768;
  56.     break;
  57.     case 3:
  58. //        fichero=fopen("/usr/share/spectrum/roms/plus3.rom","r");
  59.         fichero=fopen("roms/plus3.rom","r");
  60.         size=65536;
  61.     break;
  62.     default:
  63.         fichero=NULL;
  64.         size=0;
  65.     }
  66.   
  67.     if(fichero==NULL) {
  68.         printf("Can't open ROM file\n");
  69.         exit(1);
  70.     }
  71.     fread(ordenador.memoria,size,1,fichero);
  72.     fclose(fichero);
  73.   
  74. //    fichero=fopen("/usr/share/spectrum/roms/if1-v2.rom","r"); // load Interface1 ROM
  75.     fichero=fopen("roms/if1-v2.rom","r"); // load Interface1 ROM
  76.     if(fichero==NULL) {
  77.         printf("Can't open Interface1 ROM file\n");
  78.         exit(1);
  79.     }
  80.     
  81.     fread(ordenador.shadowrom,8192,1,fichero);
  82.       fclose(fichero);
  83. }
  84.  
  85. void init_screen(int resx,int resy,int depth) {
  86.  
  87.   int retorno,bucle,bucle2;
  88.  
  89.   retorno=SDL_Init(SDL_INIT_VIDEO);
  90.   if(retorno!=0) {
  91.     printf("Can't initialize SDL library. Exiting\n");
  92.     exit(1);
  93.   }
  94.  
  95.   // screen initialization
  96.  
  97.   screen=SDL_SetVideoMode(resx,resy,depth,SDL_HWSURFACE|SDL_HWPALETTE);
  98.   if(screen==NULL) {
  99.     printf("Can't assign SDL Surface. Exiting\n");
  100.     exit(1);
  101.   }
  102.  
  103.   if(SDL_MUSTLOCK(screen)) {
  104.     ordenador.mustlock=1;
  105.     SDL_LockSurface(screen);
  106.   } else
  107.     ordenador.mustlock=0;
  108.  
  109.   // sound initialization
  110.  
  111.     if(sound_init(48000,2048)!=0) {
  112.       printf("Can't initialize sound. Exiting. (Try with 'nosound')\n");
  113.       exit(1);
  114.     }
  115.     
  116.     for(bucle2=0;bucle2<NUM_SNDBUF;bucle2++) {
  117.         sound[bucle2]=(unsigned char *)malloc(ordenador.buffer_len+4);
  118.         for(bucle=0;bucle<ordenador.buffer_len;bucle++)
  119.             sound[bucle2][bucle]=0; 
  120.     }
  121.   
  122.     if(ordenador.format)
  123.         ordenador.increment=2*ordenador.channels;
  124.     else
  125.         ordenador.increment=ordenador.channels;
  126.  
  127.     ordenador.tst_sample=3500000/ordenador.freq;
  128.     set_volume(70);
  129. }
  130.  
  131. void end_system() {
  132.     
  133.     sound_close();
  134.     
  135.     if(ordenador.mustlock)
  136.         SDL_UnlockSurface(screen);
  137.  
  138.     if(ordenador.tap_file!=NULL)
  139.         fclose(ordenador.tap_file);
  140.  
  141.     SDL_Quit();
  142. }
  143.  
  144.  
  145. int main(int argc,char *argv[]) {
  146.  
  147.     int bucle,tstados;
  148.     word PC;
  149.  
  150.     sound_type=0;
  151.     if(argc==2)
  152.         if(0==strcmp(argv[1],"nosound"))
  153.             sound_type=1;
  154.     
  155.     atexit(end_system);
  156.     init_screen(640,480,8);
  157.  
  158.     computer_init();
  159.     register_screen(screen);
  160.     ordenador.interr=0;
  161.  
  162.     ordenador.screenbuffer=ordenador.screen->pixels;
  163.     ordenador.screen_width=ordenador.screen->w;
  164.  
  165.     // assign initial values for PATH variables
  166.  
  167.     strcpy(path_snaps,getenv("HOME"));
  168.     strcat(path_snaps,"/");
  169.     strcpy(path_taps,path_snaps);
  170.     strcpy(path_mdrs,path_snaps);
  171.     ordenador.current_tap[0]=0;
  172.  
  173.     // assign random values to the memory before start execution
  174.  
  175.     for(bucle=0;bucle<196608;bucle++)
  176.         ordenador.memoria[bucle]=(unsigned char) rand();
  177.  
  178.     ordenador.tap_file=NULL;
  179.     
  180.     // we filter all the events, except keyboard events
  181.  
  182.     SDL_EventState(SDL_ACTIVEEVENT,SDL_IGNORE);
  183.     SDL_EventState(SDL_MOUSEMOTION,SDL_IGNORE);
  184.     SDL_EventState(SDL_MOUSEBUTTONDOWN,SDL_IGNORE);
  185.     SDL_EventState(SDL_MOUSEBUTTONUP,SDL_IGNORE);
  186.     SDL_EventState(SDL_JOYAXISMOTION,SDL_IGNORE);
  187.     SDL_EventState(SDL_JOYBALLMOTION,SDL_IGNORE);
  188.     SDL_EventState(SDL_JOYHATMOTION,SDL_IGNORE);
  189.     SDL_EventState(SDL_JOYBUTTONDOWN,SDL_IGNORE);
  190.     SDL_EventState(SDL_JOYBUTTONUP,SDL_IGNORE);
  191.     SDL_EventState(SDL_QUIT,SDL_IGNORE);
  192.     SDL_EventState(SDL_SYSWMEVENT,SDL_IGNORE);
  193.     SDL_EventState(SDL_VIDEORESIZE,SDL_IGNORE);
  194.     SDL_EventState(SDL_USEREVENT,SDL_IGNORE);
  195.  
  196.     SDL_ShowCursor(SDL_DISABLE);
  197.  
  198.     salir=1;
  199.   
  200.     ResetComputer();
  201.   
  202.     procesador.Trace=0;
  203.     procesador.IAutoReset=0;
  204.     procesador.TrapBadOps=0;
  205.  
  206.     procesador.ICount=200; // to ensure that all operations will be done
  207.     procesador.TStates=0;
  208.  
  209.     sleep(1);
  210.  
  211.     clean_screen();
  212.  
  213.     microdrive_init();
  214.  
  215.     while(salir) {
  216.  
  217.         PC=ExecZ80(&procesador);
  218.         
  219.         /* if PC is 0x0556, a call to LD_BYTES has been made, so if
  220.         FAST_LOAD is 1, we must load the block in memory and return */
  221.  
  222.         if((!ordenador.mdr_paged)&&(PC==0x0556) && (ordenador.tape_fast_load==1)&&(ordenador.tape_file_type==TAP_TAP)) {
  223.             if(ordenador.tap_file!=NULL)
  224.                 fastload_block(ordenador.tap_file);
  225.             else {
  226.                 sprintf(ordenador.osd_text,"No TAP file selected");
  227.                 ordenador.osd_time=50;
  228.             }
  229.         }
  230.         
  231.         /* if PC is 0x04C2, a call to SA_BYTES has been made, so if
  232.         we want to save to the TAP file, we do it */
  233.         
  234.         if((!ordenador.mdr_paged)&&(PC==0x04C2)&&(ordenador.tape_write==1)&&(ordenador.tape_file_type==TAP_TAP)) {
  235.             if(ordenador.tap_file!=NULL)
  236.                 save_file(ordenador.tap_file);
  237.             else {
  238.                 sprintf(ordenador.osd_text,"No TAP file selected");
  239.                 ordenador.osd_time=50;
  240.             }
  241.         }
  242.         
  243.         /* if ordenador.mdr_paged is 2, we have executed the RET at 0x0700, so
  244.         we have to return to the classic ROM */
  245.         
  246.         if(ordenador.mdr_paged==2)
  247.             ordenador.mdr_paged=0;
  248.         
  249.         /* if PC is 0x0008 or 0x1708, and we have a microdrive, we have to page
  250.         the Interface 1 ROM */
  251.         
  252.         if(((PC==0x0008)||(PC==0x1708))&&(ordenador.mdr_active))
  253.             ordenador.mdr_paged = 1;
  254.         
  255.         /* if PC is 0x0700 and we have a microdrive, we have to unpage
  256.         the Interface 1 ROM after the last instruction */
  257.         
  258.         if((PC==0x0700)&&(ordenador.mdr_active))
  259.             ordenador.mdr_paged = 2;
  260.         
  261.         tstados=procesador.TStates;
  262.         if(tstados==0)
  263.             tstados=4; // if we are in halt, we execute virtual NOPs
  264.         if(tstados<0) {
  265.             printf("Error\n");
  266.             exit(1);
  267.         }
  268.         emulate(tstados); // execute the whole hardware emulation
  269.  
  270.         procesador.ICount=200; // to ensure that all operations will be done
  271.         procesador.TStates=0;
  272.  
  273.         if(ordenador.interr==1) {
  274.             IntZ80(&procesador,bus_empty()); // execute the interruption
  275.             ordenador.interr=0;
  276.         }
  277.     }
  278.     return 0;
  279. }
  280.